define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Secondhand, GoAddress,GoDialog, $stateParams, $window, $timeout, $ionicSideMenuDelegate, GoPage, GoCatTypeAuth, Auth) {
		//商品类型:二手产品为9，租赁产品为10Unsalableused
		$scope.pageType = $stateParams.pageType;
		
		$scope.datas = new Object();
		//初始值设置
		//默认排序为价格从高到低:4。价格从低到高:5
		$scope.input = new Object();
		$scope.input.nPage = 1;
		$scope.input.key = '';
		$scope.orderby = 1;
		//地区格式为字符串，形如：region_A_01,region_B_01,region_C_01
		$scope.region = '';
		//品牌格式为字符串，形如：store_cat_A_01,store_cat_B_01,store_cat_C_01
		$scope.store_cat = '';
		$scope.act_id = '';
		$scope.input.province = {id:0,name:"请选择省份"}; //省市区三级联动
		$scope.input.city = {id:0,name:"请选择城市"}; //省市区三级联动
		$scope.input.district = {id:0,name:"请选择区/县"}; //省市区三级联动
		$scope.count = null;
		
		
		if($scope.pageType==9){
			$scope.title = "二手产品";
		}else{
			$scope.title = "租赁产品";
		}

		
		$scope.isLoad = false;
		//下拉侦听，目前只能省市区三级联动
		$scope.listen_province = function(data, obj) {
			$scope.input.city = {id:0,name:"请选择城市"}; //省市区三级联动
			$scope.input.district = {id:0,name:"请选择区/县"}; //省市区三级联动
			$scope.input.district_items = null;
		};
		$scope.listen_city = function(data, obj) {
			$scope.input.district = {id:0,name:"请选择区/县"}; //省市区三级联动
		};

		//定义绑定
		var items = new Array();
		var obj1 = { name: 'input.province' };
		var obj2 = { name: 'input.city' };
		var obj3 = { name: 'input.district' };
		items.push(obj1);
		items.push(obj2);
		items.push(obj3);
		GoAddress.init_load($scope, items);

		/***分类三级联动***/
		$scope.input.brand_id = "0"; //这是品牌下拉框的绑定数据
		$scope.input.store_id = Auth.get_user_id(); //隐藏的企业id的绑定数据

		$scope.input.cat1 = { id: 0, name: "请选择大分类" };
		$scope.input.cat2 = { id: 0, name: "请选择中分类" };
		$scope.input.cat3 = { id: 0, name: "请选择小分类" };
		//下拉侦听，目前只能省市区三级联动
		$scope.listen_cat1 = function(data) {
			$scope.input.cat2 = { id: 0, name: "请选择中分类" };
			$scope.input.cat3 = { id: 0, name: "请选择小分类" };
			$scope.cat3_items = null;
		};
		$scope.listen_cat2 = function(data) {
			$scope.input.cat3 = { id: 0, name: "请选择小分类" };
		};
		//定义绑定
		var items_cat = new Array();
		var obj1_cat = { name: 'input.cat1' };
		var obj2_cat = { name: 'input.cat2' };
		var obj3_cat = { name: 'input.cat3' };
		items_cat.push(obj1_cat);
		items_cat.push(obj2_cat);
		items_cat.push(obj3_cat);
		GoCatTypeAuth.init_load_only($scope, items_cat);

		/****************上下拉刷新**********************/
		$scope.doRefresh = function() {
			$scope.isLoad = true;
			Secondhand.getlist(1, $scope.pageType, $scope.input.key, $scope.orderby, $scope.region, $scope.store_cat, $scope.act_id, function(data) {
				if(data.data.length > 0)
					$scope.datas = data;
				else
					$scope.datas = new Object();
				$scope.input.nPage = 1;
			}, function() {
				$scope.$broadcast('scroll.refreshComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1000);
			});

		};
		//加载更多
		$scope.doRefreshMore = function() {
			$scope.isLoad = true;
			if($scope.datas.data == undefined)
				$scope.input.nPage = 0;
			else
			if($scope.datas.page.count <= $scope.datas.data.length) {
				$scope.$broadcast('scroll.infiniteScrollComplete');
				if($scope.datas.page.count<$scope.input.nPage*20){
					$scope.input.nPage -=1;
					$scope.count = $scope.input.nPage *20;
				}else{
					$scope.count = null;
				}
			}
			Secondhand.getlist($scope.input.nPage + 1, $scope.pageType, $scope.input.key, $scope.orderby, $scope.region, $scope.store_cat, $scope.act_id, function(data) {
				if(data.data.length > 0) {
					if($scope.datas.data != undefined) {
						if($scope.count!=null)
						$scope.datas.data.splice($scope.count,$scope.datas.page.count-$scope.count);
						$scope.datas.data = $scope.datas.data.concat(data.data);
						$scope.input.nPage += 1;
					} else {
						$scope.datas = data;
						$scope.input.nPage = 1;
					}
				}
			}, function() {
				$scope.$broadcast('scroll.infiniteScrollComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1000);
			})
		};
		/****************上下拉刷新**********************/
		
		$scope.get_brand = function() {
			$scope.store_cat = '';
			if($scope.input.cat1&&$scope.input.cat1.id!=0) {
				$scope.store_cat = $scope.input.cat1.id;
			}
			if($scope.input.cat2&&$scope.input.cat2.id!=0) {
				$scope.store_cat += ',' + $scope.input.cat2.id;
			}
			if($scope.input.cat3&&$scope.input.cat3.id!=0) {
				$scope.store_cat += ',' + $scope.input.cat3.id;
			}
		}
		$scope.get_region = function() {
			$scope.region = '';
			if($scope.input.province&&$scope.input.province.id!=0) {
				$scope.region = $scope.input.province.id;
			}
			if($scope.input.city&&$scope.input.city.id!=0) {
				$scope.region = $scope.input.province.id + ',' + $scope.input.city.id;
			}
			if($scope.input.district&&$scope.input.district.id!=0) {
				$scope.region = $scope.input.province.id + ',' + $scope.input.city.id + ',' + $scope.input.district.id;
			}
		}
		//查询，排序。 1：默认排序    4：价格从高到低   5：价格从低到高
		$scope.doSearch = function(orderby) {
			if(orderby==1){
				$scope.orderby = 1;
			}
			if(orderby>1){//即为4或5
				$scope.orderby = $scope.orderby==4?5:4;
			}
			$scope.get_brand();//获取品牌
			$scope.get_region();//获取地区
			$scope.doCancel();
			$scope.init_data();//初始化数据
		};
		
		//侧边菜单
		$scope.rightSideMenu = function() {
			$ionicSideMenuDelegate.toggleRight();
		};
		$scope.doCancel = function(){
			if($ionicSideMenuDelegate.isOpenRight()){
				$ionicSideMenuDelegate.toggleRight();
			}
		}
		
		$scope.init_data = function() {
			Secondhand.getlist(1, $scope.pageType, $scope.input.key, $scope.orderby, $scope.region, $scope.store_cat, $scope.act_id, function(data) {
//				console.log(data)
				if(data.data.length > 0)
					$scope.datas = data;
				else
					$scope.datas = new Object();
				$scope.input.nPage = 1;
			})
		}
		
		//页面跳转
		$scope.onshow_post = function() {
			GoPage.go_states("newproductpost");
		}
		$scope.onShow_detail = function(goods_id) {
			GoPage.go_states("productdetail", { "goods_id": goods_id, "type": "goods" })
		}
		$scope.onshow_first = function(store_id) {
			GoPage.go_states("first", { store_id: store_id });
		}
		$scope.clear_key = function(){
			$scope.input.key = '';
		}
		$scope.init_data();//初始化数据
		$scope.doSelectCat1 = function(){
			//id是值 name是显示
			GoDialog.showSelect("大分类",$scope.cat1_items,$scope.input.cat1,function(data){
				$scope.input.cat1 = data;
			});
		}
		$scope.doSelectCat2 = function(){
			//id是值 name是显示
			GoDialog.showSelect("中分类",$scope.cat2_items,$scope.input.cat2,function(data){
				$scope.input.cat2 = data;
			});
		}
		$scope.doSelectCat3 = function(){
			//id是值 name是显示
			GoDialog.showSelect("小分类",$scope.cat3_items,$scope.input.cat3,function(data){
				$scope.input.cat3 = data;
			});
		}
		$scope.doSelectProvince = function(){
			//id是值 name是显示
			GoDialog.showSelect("请选择省份",$scope.province_items,$scope.input.province,function(data){
				$scope.input.province = data;
			});
		}
		$scope.doSelectCity = function(){
			//id是值 name是显示
			GoDialog.showSelect("请选择城市",$scope.city_items,$scope.input.city,function(data){
				$scope.input.city = data;
			});
		}
		$scope.doSelectDistrict = function(){
			//id是值 name是显示
			GoDialog.showSelect("请选择区/县",$scope.district_items,$scope.input.district,function(data){
				$scope.input.district = data;
			});
		}
		
	}

	controller.$inject = ['$scope', 'Secondhand', 'GoAddress','GoDialog', '$stateParams', '$window', '$timeout', '$ionicSideMenuDelegate', 'GoPage', 'GoCatTypeAuth', 'Auth'];

	return app.registerController("secondhandctrl",controller);
});